#version 330 core

in vec3 FragPos;
in vec3 Normal;
in vec2 TexCoord;

out vec4 FragColor;

uniform sampler2D texture_sampler;
uniform bool hasTexture;

void main()
{
    vec3 lightDir = normalize(vec3(1.0, 1.0, 1.0));
    float diff = max(dot(normalize(Normal), lightDir), 0.3);
    vec3 lightColor = vec3(1.0, 1.0, 1.0);

    vec4 texColor;
    if (hasTexture) {
        texColor = texture(texture_sampler, TexCoord);
    } else {
        // 如果没有纹理，使用默认的蓝色
        texColor = vec4(0.0, 0.4, 0.8, 1.0);
    }

    vec3 result = texColor.rgb * diff * lightColor;

    // 添加一些镜面高光
    vec3 viewDir = normalize(-FragPos);
    vec3 reflectDir = reflect(-lightDir, normalize(Normal));
    float spec = pow(max(dot(viewDir, reflectDir), 0.0), 32.0);
    result += spec * 0.3 * lightColor;

    FragColor = vec4(result, 1.0);
}
